package com.kmzx.one.project.controller;

import com.kmzx.one.common.utils.MyAssert;
import com.kmzx.one.project.net.BaseResult;
import com.kmzx.one.project.others.dto.FansInfo;
import com.kmzx.one.project.others.dto.UserRankInfo;
import com.kmzx.one.project.others.dto.UserRankMoreInfo;
import com.kmzx.one.project.service.UserFanService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Comparator;
import java.util.HashSet;
import java.util.List;

@Api(tags = "快美甄选_[ 粉丝相关_接口 ]")
@RestController
public class UserFansController extends BaseController {

    @Autowired
    UserFanService userFanService;


    @ApiOperation(value = "1.我的直属粉丝列表", notes = "我的直属粉丝列表")
    @GetMapping(value = "fans/one", produces = "application/json;charset=UTF-8")
    public BaseResult oneFansLists(@RequestParam("pageNum") Integer pageNum,
                                   @RequestParam("pageSize") Integer pageSize,
                                   @RequestParam("userId") String userId) {
        try {
            MyAssert.notNullObject(pageNum, "pageNum 为空");
            MyAssert.notNullObject(pageSize, "pageSize 为空");
            MyAssert.notNullObject(userId, "userId 为空");
            List<FansInfo> result = userFanService.findUserFans(userId, pageNum, pageSize);
            BaseResult error = BaseResult.Builder.SUCC().initSuccData(result);
            return error;

        } catch (Exception e) {
            return handleException(e);
        }
    }


    @ApiOperation(value = "2.我断开的粉丝列表", notes = "我断开的粉丝粉丝列表")
    @GetMapping(value = "fans/before", produces = "application/json;charset=UTF-8")
    public BaseResult oneFansListsOld(@RequestParam("pageNum") Integer pageNum,
                                      @RequestParam("pageSize") Integer pageSize,
                                      @RequestParam("userId") String userId) {
        try {
            MyAssert.notNullObject(pageNum, "pageNum 为空");
            MyAssert.notNullObject(pageSize, "pageSize 为空");
            MyAssert.notNullObject(userId, "userId 为空");
            UserRankInfo re = userFanService.findUserRankTabs(userId);

            if (re != null) {
                int rankNo = re.getRankNo();
                if (rankNo == 0) {
                    BaseResult error = BaseResult.Builder.FAIL().setErrMsg("您是注册会员,暂时没有粉丝的呦!");
                    return error;
                } else {
                    List<FansInfo> result = userFanService.findUserOlderFans(userId, pageNum, pageSize);
                    BaseResult error = BaseResult.Builder.SUCC().initSuccData(result);
                    return error;
                }
            }
            BaseResult error = BaseResult.Builder.FAIL();
            return error;
        } catch (Exception e) {
            return handleException(e);
        }
    }


    @ApiOperation(value = "3.我的所有粉丝列表( banned)", notes = "我的所有粉丝列表")
    @GetMapping(value = "fans/All", produces = "application/json;charset=UTF-8")
    public BaseResult allFansLists(@RequestParam("pageNum") Integer pageNum,
                                   @RequestParam("pageSize") Integer pageSize,
                                   @RequestParam("userId") String userId,
                                   @RequestParam("kinds") Integer kinds) {
        try {
            MyAssert.notNullObject(pageNum, "pageNum 为空");
            MyAssert.notNullObject(pageSize, "pageSize 为空");
            MyAssert.notNullObject(userId, "userId 为空");
            MyAssert.notNullObject(kinds, "kinds 为空");
            List<FansInfo> result = userFanService.findUserFansPagers(userId, pageNum, pageSize, kinds);
            if (!CollectionUtils.isEmpty(result)) {
                HashSet h = new HashSet(result);
                result.clear();
                result.addAll(h);
                result.sort(Comparator.comparing(FansInfo::getCom).reversed());

//                List<FansInfo> collect = result.stream().distinct().collect(Collectors.toList());
//                result = collect;
            }
            BaseResult error = BaseResult.Builder.SUCC().initSuccData(result);
            return error;
        } catch (Exception e) {
            return handleException(e);
        }
    }

    @ApiOperation(value = "4.我的管家", notes = "我的管家")
    @GetMapping(value = "fans/manager", produces = "application/json;charset=UTF-8")
    public BaseResult getMyInvitedUser(@RequestParam("userId") String userId) {
        try {
            MyAssert.notNullObject(userId, "userId 为空");
            UserRankMoreInfo myManager = userFanService.findMyManager(userId);
            BaseResult error = BaseResult.Builder.SUCC().initSuccData(myManager);
            return error;
        } catch (Exception e) {
            return handleException(e);
        }
    }


    @ApiOperation(value = "5.我的全部粉丝", notes = "我的全部粉丝")
    @GetMapping(value = "fans/entry", produces = "application/json;charset=UTF-8")
    public BaseResult allFansLists2(@RequestParam("userId") String userId, @RequestParam("pageSize") Integer pageSize,
                                    @RequestParam("pageNum") Integer pageNum) {
        try {
            MyAssert.notNullObject(userId, "userId 为空");
            pageNum = pageNum == null ? super.pageNum : pageNum;
            pageSize = pageSize == null ? super.pageSize : pageSize;
            List<FansInfo> infos = userFanService.findUserFansEntry(userId, pageNum, pageSize);
            BaseResult error = BaseResult.Builder.SUCC().initSuccData(infos);
            return error;
        } catch (Exception e) {
            return handleException(e);
        }
    }


}
